package org.xiaom.butler.activity;

import java.util.HashMap;
import java.util.Map;

import org.xiaom.butler.R;
import org.xiaom.butler.bean.Hotel;
import org.xiaom.butler.dbService.HotelService;
import org.xiaom.butler.util.AppHelper;
import org.xiaom.butler.util.DataManager;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MenuItem.OnMenuItemClickListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;
public class MainActivity extends Activity {
	public static final int INIT_DATA_SUCCESS = 1;
	public static final int INIT_DATA_FAILED = 2;
	final Map<String,Object> modelAndView=new HashMap<String,Object>();
	HotelService hs=null;
	View butler;
	View takeMe;
	View weather;
	Handler hander = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case INIT_DATA_SUCCESS:
				initView();
				break;
			case INIT_DATA_FAILED:
				exit();
				break;
			default:
				break;
			}
		}
	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		hs=new HotelService(this);
		setContentView(R.layout.wait);
		initData();               
	}

	boolean initData() {
		Boolean rs = false;
		try {
			new Thread() {
				public void run() {
					try{
						SharedPreferences sharedPreferences=getSharedPreferences(AppHelper.SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
						SharedPreferences.Editor editor=sharedPreferences.edit();
						int count=sharedPreferences.getInt(AppHelper.USE_COUNT, 0);
						Hotel hotel=null;
						if(count==0){//第一次运行app
							AppHelper.initApp(MainActivity.this);
							DataManager.clearAll(MainActivity.this);
							hotel=DataManager.getDataAndSave(MainActivity.this);
						}else{
							hotel=hs.getCurrentHotel();
						}
						editor.putInt(AppHelper.USE_COUNT,++count);
						editor.commit();
						if(hotel==null){
							throw new Exception(getString(R.string.system_init_data_failed));
						}
						modelAndView.put("hotel", hotel);
						hander.sendEmptyMessage(INIT_DATA_SUCCESS);
					} catch (Exception e) {
						e.printStackTrace();
						hander.sendEmptyMessage(INIT_DATA_FAILED);
					}
				};
			}.start();
		} catch (Exception e) {
		}
		return rs;
	}

	private void initView() {
		setContentView(R.layout.main);
		butler =  findViewById(R.id.btn_enter);
		takeMe = findViewById(R.id.btn_take_me);
		weather=findViewById(R.id.btn_weather);
		final Hotel h=(Hotel) modelAndView.get("hotel");
		findViewById(R.id.main_index).setBackgroundDrawable(Drawable.createFromPath(h.getImagePath()));
		butler.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(
						Intent.ACTION_CALL,	Uri.parse("tel:"+ getSharedPreferences(AppHelper.SHARED_PREFERENCES_NAME,
						MODE_PRIVATE).getString(AppHelper.SP_CURRENT_HOTEL_PHONE,getString(R.string.app_init_hotel_phone))));
				startActivity(intent);
			}
		});
		takeMe.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent i = new Intent(MainActivity.this, ItemListActivity.class);
				startActivity(i);
			}
		});
		weather.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent intent=new Intent(MainActivity.this, WeatherActivity.class);
				intent.putExtra("hotel", h);
				startActivity(intent);
			}
		});
	}
	private void updateData() {
		this.setContentView(R.layout.wait);
		new Thread() {
			public void run() {
				int rs = INIT_DATA_SUCCESS;
				if (DataManager.checkUpdate(MainActivity.this)) {
					try {
						DataManager.clearAll(MainActivity.this);
						modelAndView.put("hotel", DataManager.getDataAndSave(MainActivity.this));
					} catch (Exception e) {
						rs = INIT_DATA_FAILED;
						e.printStackTrace();
					}
				}
				hander.sendEmptyMessage(rs);
			};
		}.start();
	}
	private void exit() {
		Toast.makeText(this, getString(R.string.system_init_data_failed), 5000).show();
		finish();
	}
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater menuInflater=new MenuInflater(this);
		menuInflater.inflate(R.menu.menu_main, menu);
		menu.findItem(R.id.menu_main_update_data).setOnMenuItemClickListener(new OnMenuItemClickListener() {
			public boolean onMenuItemClick(MenuItem item) {
				updateData();
				return false;
			}
		});
		menu.findItem(R.id.menu_main_update_app).setOnMenuItemClickListener(new OnMenuItemClickListener() {
			@Override
			public boolean onMenuItemClick(MenuItem item) {
				new Thread(){
					@Override
					public void run() {
						AppHelper.updateApp(MainActivity.this);
					}
				}.start();
				return false;
			}
		});
		return super.onCreateOptionsMenu(menu);
	}
}